
/*******************************************************
*  Team name: FTPMP 
*  Class involve: Board, Player, Map, MagicTool, Event, SA
*  Version, Author, History of modification: 
*  Ver 1.0, Ada,
*  -add order, winCon, MTCardNo, players, map, tempMT, MTPool
*  
*  Ver 1.1, Ada,
*  -add Board Constructor, getPlayerMT, getCard, assignRole
*  
*  Ver 1.2, Ada,
*  -add setSE, setPlayerNo, drawPlayerMT, getSABoxList, getMTBoxList
*  
*******************************************************/

package entity;
import java.awt.*;
import javax.swing.*;

import java.lang.reflect.Array;
import java.util.Vector;


public class Board 
{
	int[] order;
	int winCon;
	int MTCardNo;
	Player[] players;
	Map map;
	// cards for draw
	MagicTool tempMT;
	MagicTool[] MTPool;
	Event[] eventPool;
	SA[] SAPool;
	int playerNo;
	public Board()
	{
		map=new Map();
		setEvtPool();
		setSAPool();
		setMTPool();	
	}
	private void setEvtPool()
	{
		eventPool=new Event[20];
		String[] temp_opt = {"<html>A: \u61C9\u627F\u53BB\uFF0C\u4F46\u6253\u7B97\u8AAA\u8B0A\u56E0<br />\u70BA\u5176\u4ED6\u7279\u5225\u4E8B\u60C5\uFF0C\u800C\u4E0D\u80FD\u53C3\u8207\u3002</html>", "<html>B: \u96D6\u7136\u60F3\u53BB\u4F46\u62D2\u7D55\u53C3\u8207\uFF0C<br />\u56E0\u7232\u6015\u7236\u6BCD\u4E0D\u958B\u5FC3\u800C\u63A8\u537B\u4E0D\u53C3\u8207\u3002</html>", "<html>C: \u8207\u7236\u6BCD\u89AA\u8A0E\u8AD6\u6EAB\u7FD2\u6642\u9593\uFF0C<br />\u5236\u5B9A\u65B0\u65F3\u6E29\u7FD2\u6642\u9593\u8868\uFF0C<br />\u4E26\u8868\u9054\u81EA\u5DF1\u7684\u610F\u9858\u3002</html>", "D: \u554F\u5144\u59CA\u7684\u610F\u898B\u3002"};
		String[] temp_hint = {"e", "f", "g", "h"};
		int[] temp_reward = {-2, -1, 2, 1};
		eventPool[0]=new Event("<html>\u4ED4\u4ED4\uFF08\u5973\u5973\uFF09\u5B78\u6821\u7684\u7D42\u671F\u8003\u8A66\u81E8\u8FD1\uFF0C\u7236\u89AA\u56E0\u70BA\u4ED4\u4ED4\uFF08\u5973\u5973\uFF09\u5728\u4E0A\u5B78\u671F<br />\u7684\u4E2D\u6587\u8003\u8A66\u6210\u7E3E\u5F97\u5230\u516D\u5341\u5206\u800C\u7981\u6B62\u4ED6\uFF0F\u5979\u65BC\u661F\u671F\u516D\u51FA\u5916\u3002\u4F46\u662F\u9019\u661F\u671F<br />\u516D\uFF08\u5973\u5973\uFF09\u7684\u597D\u540C\u5B78\u9080\u8ACB\u4ED6\uFF0F\u5979\u51FA\u5E2D\u751F\u65E5\u6703\u3002\u5ABD\u5ABD\u8ACB\u6C7A\u5B9A\u5982\u4F55\u89E3\u6C7A\u9019\u4E8B\u60C5\uFF1F</html>", temp_reward, temp_opt, temp_hint);
	}
	private void setSAPool()
	{
		String temp_info = "\u5411\u5176\u4E2D\u4E00\u4F4D\u5BB6\u4EBA\u8B1B\u4E00\u53E5\u6B23\u8CDE\u7684\u8AAA\u8A71\u3002";
		SAPool=new SA[20];
		SAPool[0] = new SA("\u8AAA\u8A71", 5, temp_info);
	}
	private void setMTPool()
	{
		MTPool= new MagicTool[3];
		MTPool[0]=new MagicTool("Forward", "Forward Card:  You can move forward for 3 steps immediately");
		MTPool[1]=new MagicTool("Bonus", "Bonus Card: When in effect, the reward earned from event or SA will be doubled.");
		MTPool[2]=new MagicTool("Skip", "Skip Card: You can skip an event or SA.");
		this.MTCardNo=0;
	}
	
	public Map getMap()
	{
		return this.map;
	}
	/*func called by BoardEngine for getting MT types
	*/
	
	public String[] getPlayerMT(int PID)
	{
		String[] MTType;
		
		int MTNo=players[PID].getMagicToolNum();
		MTType=new String[MTNo];
		//get the list of MT of a player 
		Vector<MagicTool> MT=players[PID].getMagicTool();
		// get all types of the MT
		System.out.println("Player: "+(PID+1)+" MagicToolNo: "+MTNo);
		for(int i=0;i<MTNo;i++)
		{
			MTType[i]=MT.get(i).getType();
			System.out.println("Player: "+(PID+1)+" MagicToolNo: "+MTType[i]);
		}
		return MTType;
	}
	
	//func for draw SA/Event
	public Card getCard(String type, int cardNo)
	{
		
		if(type=="Event")
		{
			Event ev = eventPool[cardNo];
			//System.out.println(ev.getEID());
			return ev;
		}
		else
		{
			SA sa = SAPool[cardNo];
			return sa;	
		}
			//return SAPool[(int)Math.random()*20];
		
	}
	public Event[] getEvPool()
	{
		return this.eventPool;
	}
	public SA[] getSAPool()
	{
		return this.SAPool;
	}
	public MagicTool[] getMTPool()
	{
		return this.MTPool;
	}
	public void assignRole(String[]roleList)
	{
		for(int i=0;i<playerNo;i++)
		{
			players[i].setRole(new Role(roleList[i]));
		}
	}
	
	public int tempSE(int pid, int reward)
	{
		int temp_se = this.players[pid].getSE()+reward;
		if(temp_se>40)
			temp_se=40;
		else if(temp_se<0)
			temp_se=0;
		else
			return temp_se;
		
		return temp_se;
	}
	public void giveReward(int pid, int cardID, int ans, String type)
	{
		int se=0;
		if(type.equals("control.EventCardController"))
			se = tempSE(pid, eventPool[cardID].getReward(ans)*this.players[pid].getBonus());
		else
			se = tempSE(pid, SAPool[cardID].getReward(ans)*this.players[pid].getBonus());
		
		this.players[pid].setSE(se);
	}
	
	//func for event
	//pending for refactoring

	
	public void initPlayerList(int playerNo)
	{
		this.playerNo=playerNo;
		this.players=new Player[this.playerNo];
		System.out.println("this is board: "+this.playerNo);
		for(int i=0;i<this.playerNo;i++)
		{
			players[i]=new Player(i);
			System.out.println("this is board player: "+this.playerNo);
		}
	}
	
	public void drawPlayerMT(int PID)
	{
		MagicTool temp=MTPool[(int)(Math.random()*3)];

		players[PID].addMagicTool(temp);
		System.out.println("Player: "+PID+" draw Player MT: "+temp.getType());
	}
	
	public void drawPlayerMT2(int PID) 
	{
		for(int i=0; i<3; i++)
		{
			MagicTool temp=MTPool[i];
			players[PID].addMagicTool(temp);
			System.out.println("Player: "+PID+" draw Player MT: "+temp.getType());
		}	
	}
	public void withdrawPlayerMT(int PID, int MTID)
	{
		players[PID].removeMagicTool(MTID);
	}
	
	public String validateBoxType(int BoxNo)
	{
		if(this.map.validateEventBox(BoxNo))
			return "Event";
		else if(this.map.validateSABox(BoxNo))
			return "SA";
		else if(this.map.validateMTBox(BoxNo))
			return "MT";
		else
			return "Main";
	}

	public String getMagicToolDesp(int currentPlayer, int MTID) 
	{
		String desp = this.players[currentPlayer].getMTInfo(MTID);
		return desp;
	}

	public int[] getPlayerSE() 
	{
		int[] se = new int[this.players.length];
		
		for(int i=0; i<se.length; i++)
		{
			se[i]=this.players[i].getSE();
		}
		return se;
	}

	public int getPlayerMTNo(int PID)
	{
		return players[PID].getMagicToolNum();
	}
	
	public void removeUsedMT(int PID, int MTID) 
	{
		this.players[PID].removeMagicTool(MTID);
		
	}
	//for winning method 2
	public boolean validateTotalSE() 
	{
		int totalSE=0;
		for(int i=0;i<this.playerNo;i++)
			totalSE+=this.players[i].getSE();

		return (this.winCon<=totalSE);
	}
	//for winning method 1
	/*
	 * have to check if players got same SE
	 */
	public int validateWinCon() 
	{
		for(int i=0;i<this.playerNo;i++)
		{
			if(this.players[i].getSE()>=this.winCon)
				return i;
		}
		return -1;
	}
	
	public void setWinCon(int winCon) 
	{
		this.winCon=winCon;
	}
	
	public void addCurrentBonus(int currentPlayer) 
	{
		this.players[currentPlayer].updateBonus(this.players[currentPlayer].getBonus()*2);
		
		System.out.println("Board- addCurrentBonus-player:  "+this.players[currentPlayer]
		+"bonus: "+this.players[currentPlayer].getBonus());
	}
	
	public void clearCurrentBonus(int currentPlayer)
	{
		this.players[currentPlayer].updateBonus(1);
	}

	public Player[] getPlayers() 
	{
		return this.players;
	}
	// Added for test case "testAddBonus"
	// 9April 2012, Oscar
	public int[] getPlayerBonus() 
	{
		int[] bonus = new int[this.players.length];
		
		for(int i=0; i<bonus.length; i++)
		{
			bonus[i]=this.players[i].getBonus();
		}
		return bonus;
	}
	
	public void drawPlayerMTforTest(int PID)
	{
		MagicTool temp=MTPool[(int)0];

		players[PID].addMagicTool(temp);
		System.out.println("Player: "+PID+" draw Player MT: "+temp.getType());
	}
	
}
